﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private List<ErrorCode> list = new List<ErrorCode>();
        private void Form1_Load(object sender, EventArgs e)
        {
            string filePath = @"错误编码.txt";
            if (!File.Exists(filePath))
            {
                MessageBox.Show("[错误编码.txt]文件不存在，请将该文件放在程序同目录下！", "文件未找到", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            using (StreamReader sr = new StreamReader(filePath))
            {
                try
                {
                    while (true)
                    {
                        string str = sr.ReadLine();
                       //读取文件为空跳出循环
                        if (str == null)
                        {
                            break;
                        }
                        var li = str.Split('|');
                        ErrorCode ec = new ErrorCode() { Id = li[1].Trim(), Code = li[2].Trim(), Explain = li[3].Trim() };
                        list.Add(ec);

                    }
                    dataGridView1.DataSource = new List<ErrorCode>(list);
                }
                catch (Exception)
                {
                    MessageBox.Show("请确认[错误编码.txt]文件格式\n正确格式： ·错误：| ID | 编码 | 解释", "格式错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// 更新查询数据
        /// </summary>
        /// <param name="id">查询id</param>
        private void Update(string id)
        {
            List<ErrorCode> li = new List<ErrorCode>();
            foreach (var item in list)
            {
                if (item.Id.Contains(id))
                {
                    li.Add(item);
                }
            }
            dataGridView1.DataSource = new List<ErrorCode>(li);
        }


        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            Update(textBox1.Text.Trim());
        }


    }
    /// <summary>
    /// 错误代码对象
    /// </summary>
    class ErrorCode
    {
        public string Id { get; set; }
        public string Code { get; set; }
        public string Explain { get; set; }
    }
}
